<route lang="json5">
{
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '毕业院校',
  },
}
</route>

<template>
  <view class="absolute left-0 right-0 top-0 bottom-0">
    <z-paging 
      ref="pagingRefs" 
      v-model="schoolList" 
      :default-page-size="20"
      use-virtual-list
      :safe-area-inset-bottom="true"
      :auto="false"
      @query="queryList"
    >
      <template #top>
        <!-- 导航 -->
        <!--  #ifdef MP-WEIXIN -->
        <wd-navbar 
          placeholder 
          safeAreaInsetTop 
          :title="themeConfig.title" 
          left-arrow 
          @click-left="back"
        ></wd-navbar>
        <!--  #endif -->
        <wd-search 
          v-model="searchText" 
          cancel-txt="搜索"
          @search="handleSearch" 
          @cancel="handleSearch"
          @clear="handleSearch"
        />
      </template>
      <template v-slot:cell="{item,index}">
        <wd-cell 
          :title="item.typeName" 
          value="" 
          clickable 
          @click="selectedSchool(item)"
        />
      </template>
    </z-paging>
  </view>
</template>
  
<script lang="ts" setup>
import { Main } from "@/service/types";
import { useCommonStore, useThemeStore, useUserStore } from "@/store";
import { back } from "@/utils";
import { deepClone } from "wot-design-uni/components/common/util";

// 主题颜色
const { themeConfig } = useThemeStore();
const useCommon = useCommonStore();
const user = useUserStore();

const schoolList = ref([]);
const pagingRefs = ref();
const queryList = () => {
  const list = getSchoolList();
  pagingRefs.value.setLocalPaging(list);
}
const handleSearch = () => {
  pagingRefs.value.reload();
}

const searchType = ref('');
const searchText = ref('');
const getSchoolList = (): Main.DictInfo[] => {
  const list = deepClone(useCommon.commonState.dictInfo[searchType.value]) || [];
  
  if (isNoLimit.value === 1) {
    list.unshift({
      id: '1839565474554',
      typeCode: '',
      typeName: '不限',
    })
  }
  const _list = list.filter(item => item.typeName.indexOf(searchText.value) >= 0);
  return _list
}

const selectedSchool = (item) => {
  // user.heightInfo[searchType.value].label = item.typeName;
  // user.heightInfo[searchType.value].value = item.typeCode;
  
  uni.$emit(`updateSchool${pageName.value}`, JSON.stringify({
    type: searchType.value,
    data: item
  }));
  
  back();
}

const titleOption = {
  school: '毕业院校',
  profession: '行业/职业'
}

onShow(() => {
  nextTick(() => {
    // 设置页面标题
    themeConfig.title = titleOption[searchType.value] || '毕业院校';
  })
})

const pageName = ref('');
const isNoLimit = ref(0);
onLoad((options) => {
  const { type, typeCode, isNo, page } = options || {};
  pageName.value = page || '';
  searchType.value = type || 'school';
  isNoLimit.value = Number(isNo) || 0;

  const list = useCommon.commonState.dictInfo[searchType.value] || [];
  const item = list.find(item => item.typeCode === typeCode) || { typeName: '' };
  searchText.value = item.typeName || '';
  
  nextTick(() => {
    if (!useCommon.hasDictInfo) {
      useCommon.dictInfoCallback.push(pagingRefs.value.reload);
    }
  })

  setTimeout(() => {
    pagingRefs.value.reload();
  }, 0)
})
</script>

<style lang="scss" scoped>
</style>
        